package com.example.zhyq.park

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.ParkBeanItem
import com.example.zhyq.R
import com.example.zhyq.databinding.ItemParkBinding

class ParkPageAdapter : PagingDataAdapter<ParkBeanItem, ParkPageAdapter.ParkViewHolder>(diff) {

    private var mOnItemClickListener: OnItemClickListener?=null
    private var mOnItemLongClickListener : OnItemLongClickListener?=null

    companion object{
        val diff = object : ItemCallback<ParkBeanItem>(){
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
               return oldItem.spaceStatus==newItem.spaceStatus
            }

        }
    }

    class ParkViewHolder(itemView:View):ViewHolder(itemView){
        val binding = DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val bean = getItem(position)!!
        holder.binding?.apply {
            parkNum.setText(bean.spaceNumber)
            parkArea.setText("所属区域:${bean.areaName}")
            parkType.setText("车位类型:${bean.spaceType}")
            parkUser.setText("使用人:${bean.userName}")
            parkCarNo.setText("车牌号:${bean.licensePlate}")
            parkStatus.setText(bean.spaceStatus)

            if (bean.spaceStatus=="空闲"){
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.green))
                parkStatus.setBackgroundResource(R.drawable.shape_status)
            }else if (bean.spaceStatus=="已占用"){
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.blue))
                parkStatus.setBackgroundResource(R.drawable.shap_zhanyong)
            }else {
                parkStatus.setTextColor(holder.itemView.context.getColor(R.color.orange))
                parkStatus.setBackgroundResource(R.drawable.shape_weihu)
            }
        }


        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position,bean)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(bean)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(view)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mOnItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener) {
        this.mOnItemLongClickListener = listener
    }

    interface OnItemClickListener{
        fun onItemClick(position: Int,item: ParkBeanItem)
    }

    interface OnItemLongClickListener{
        fun onItemLongClick(item: ParkBeanItem)
    }
}